package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jwgf.Half485.modbusthreadpool.ModbusCommandManager;
import com.jwgf.common.Utility;
import com.jwgf.common.ViewCache;
import com.jwgf.group.Group;
import com.jwgf.group.GroupMap;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.SpindleParameter;
import com.jwgf.protocolstack.CommunicationStateListener.WriteFailedListener;
import com.jwgf.protocolstack.CommunicationStateListener.WriteSucceedListener;
import com.jwgf.protocolstack.ProtocolStack;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.CanCommand;
import com.jwgf.smarowindingmachine_unity.logic.CommandGenerator;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;
import com.jwgf.smarowindingmachine_unity.logic.Report;
import com.jwgf.smarowindingmachine_unity.logic.SmaroWindingMachineDataAccessRules;
import com.jwgf.smarowindingmachine_unity.logic.UnitUtils;
import com.jwgf.smarowindingmachine_unity.ui.ObservableScrollView.OnScrollChangedListener;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.QueryFilter;

public class ParameterSettingSpindleParameterFragment_craft extends BaseFragment implements OnScrollChangedListener,
        OnClickListener, OnEditorActionListener, OnFocusChangeListener, WriteSucceedListener, WriteFailedListener {
    public static final String fragmentTag = "ParameterSettingSpindleParameterFragment";
    private View v;
    private RelativeLayout layoutSeperator1;
    private RelativeLayout layoutSeperator2;
    private RelativeLayout layoutSeperator3;
    private RelativeLayout layoutSeperator4;
    private RelativeLayout layoutSeperator5;
    private RelativeLayout layoutSeperator6;
    private float pageSeperator1;
    private float pageSeperator2;
    private float pageSeperator3;
    private float pageSeperator4;
    private float pageSeperator5;
    private float pageSeperator6;

    private Button btn_work_param;
    private Button btn_cycle_param;
    private Button btn_tension_param;
    private Button btn_anti_ptn_param;
    private Button btn_splicer_param;
    private Button btn_yarn_suction_param;

    private Button btn_tens_sensor_has;
    private Button btn_tens_sensor_no;
    private Button btn_vss_has;
    private Button btn_vss_no;
    private RelativeLayout tens_up_lmt_layout;
    private RelativeLayout tens_low_lmt_layout;
    private RelativeLayout sample_yarn_num_layout;
    private RelativeLayout sample_up_lmt_layout;
    private RelativeLayout sample_low_lmt_layout;

    private RelativeLayout spd_reduce_layout;
    private RelativeLayout yarn_len_layout;
    private RelativeLayout min_tens_layout;
    private RelativeLayout splicer_param_layout;
    private EditText ends_num_edit;
    private EditText yarn_code_edit;
    private EditText pkg_wei_edit;
    private EditText cop_wei_edit;
    private EditText slip_edit;
    private EditText prod_correct_edit;
    private EditText single_strength_edit;
    private EditText cop_num_edit;
    private EditText sample_yarn_num_edit;
    private EditText sample_up_lmt_edit;
    private EditText sample_low_lmt_edit;
    private EditText tens_yarn_num_edit;
    private EditText yarn_name_value;
    private TextView group_value;
    private TextView anti_ptn_mode_value;
    private TextView anti_wrap_sense_value;
    private TextView yarn_cnt_unit;
    private TextView pkg_len_unit;
    private TextView cop_len_unit;
    private TextView pkg_wei_unit;
    private TextView cop_wei_unit;
    private TextView wind_spd_unit;
    private TextView s_drum_rev_spd_unit;
    private TextView l_drum_rev_spd_unit;
    private TextView pkg_init_spd_unit;
    private TextView wind_spd_text;
    private TextView blow_time_text;
    private TextView tens_sensor_text;
    private TextView yarn_name_text;
    private int craftCount;//
    private int grp_select = 1;
    private ImageButton btn_left_arrow, btn_right_arrow;
    //0x10
    private EditText start_acc_time_big_edit;
    private EditText start_acc_time_small_edit;

    //0x15
    private EditText arm_lift_delay;
    private TextView full_package_brake_value;
    private ImageButton btn_full_package_brake;

    //0x16
    private Button btn_action_type_ok;
    private Button btn_action_type_special;
    private CheckBox defect1;
    private CheckBox defect2;
    private CheckBox defect3;
    private CheckBox defect4;
    private CheckBox defect5;
    private CheckBox alarm1;
    private CheckBox alarm2;
    private CheckBox alarm3;
    private CheckBox alarm4;
    private CheckBox alarm5;
    private EditText suction_time_edit;
    private EditText repeat_time_edit;
    private EditText suction_stroke_edit;
    private EditText tries_limit_edit;
    private RelativeLayout yarn_suction_detail_layout;

    // 0x20
    private EditText wind_spd_edit;
    private EditText start_acc_time_middle_edit;

    // 0x21
    private EditText s_drum_rev_spd_edit;
    private EditText l_drum_rev_spd_edit;
    private EditText splice_auto_start_edit;
    private EditText cop_auto_start_edit;

    // 0x22
    private EditText cycle_num_edit;
    private EditText change_num_edit;
    private EditText pkg_num_edit;

    // 0x23
    private EditText anti_ptn_period_edit;
    private EditText acc_spd_edit;
    private EditText acc_time_edit;
    private ImageButton btn_anti_ptn_mode;

    // 0x24
    private EditText pkg_len_edit;
    private EditText cop_len_edit;
    private EditText package_diameter_radio_edit;

    // 0x25
    private EditText max_pkg_splice_edit;
    private EditText max_cop_cuts_edit;
    private EditText blow_time_edit;

    // 0x26
    private EditText yarn_len_edit;
    private EditText spd_reduce_edit;
    private ImageButton btn_anti_wrap_sense;

    // 0x27
    private EditText min_tens_edit;
    private EditText tens_sensor_edit;
    private EditText tension_edit;
    private EditText tens_increase_edit;
    private EditText tens_low_lmt_edit;
    private EditText tens_up_lmt_edit;

    // 0x28
    private EditText pkg_init_spd_edit;
    private EditText yarn_cnt_edit;
    private EditText slow_brake_dec_time_edit;

    // 0x2a
    private EditText over_spd_inc_edit;
    private EditText no_over_cycle_inc_edit;

    // 0x2C
    private ImageButton btn_splicer_length;
    private EditText splicer_time_t1_value;
    private TextView splicer_length_value;
    private EditText splicer_time_t2_value;
    // 0x2e
    private EditText tens_pad_spd_eidt;
    private EditText tension_increase_time;

    private SpindleParameter parameter;

    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    ObservableScrollView scrollView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        v = ViewCache.getInstance().getView(R.layout.fragment_parameter_setting_spindle_parameter);
        layoutSeperator1 = (RelativeLayout) v.findViewById(R.id.wind_spd_layout);
        layoutSeperator1.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator1 = layoutSeperator1.getY();

            }
        });
        layoutSeperator2 = (RelativeLayout) v.findViewById(R.id.blow_time_layout);
        layoutSeperator2.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator2 = layoutSeperator2.getY();

            }
        });
        layoutSeperator3 = (RelativeLayout) v.findViewById(R.id.tens_sensor_layout);
        layoutSeperator3.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator3.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator3 = layoutSeperator3.getY();

            }
        });
        layoutSeperator4 = (RelativeLayout) v.findViewById(R.id.anti_ptn_mode_layout);
        layoutSeperator4.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator4.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator4 = layoutSeperator4.getY();

            }
        });
        layoutSeperator5 = (RelativeLayout) v.findViewById(R.id.splicer_param_layout);
        layoutSeperator5.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator5.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator5 = layoutSeperator5.getY();

            }
        });
        layoutSeperator6 = (RelativeLayout) v.findViewById(R.id.yarn_suction_param_layout);
        layoutSeperator6.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                layoutSeperator6.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pageSeperator6 = layoutSeperator6.getY();

            }
        });
        initialViews(v);
        updateGroupChange();
        return v;
    }

    private void initialViews(View v) {
        scrollView = (ObservableScrollView) v.findViewById(R.id.spindle_parameter1);
        scrollView.setScrollChangedListener(this);
        tens_up_lmt_layout = (RelativeLayout) v.findViewById(R.id.tens_up_lmt_layout);
        tens_low_lmt_layout = (RelativeLayout) v.findViewById(R.id.tens_low_lmt_layout);
        sample_yarn_num_layout = (RelativeLayout) v.findViewById(R.id.sample_yarn_num_layout);
        sample_up_lmt_layout = (RelativeLayout) v.findViewById(R.id.sample_up_lmt_layout);
        sample_low_lmt_layout = (RelativeLayout) v.findViewById(R.id.sample_low_lmt_layout);
        yarn_suction_detail_layout = v.findViewById(R.id.yarn_suction_detail_layout);
        spd_reduce_layout = (RelativeLayout) v.findViewById(R.id.spd_reduce_layout);
        yarn_len_layout = (RelativeLayout) v.findViewById(R.id.yarn_len_layout);
        min_tens_layout = (RelativeLayout) v.findViewById(R.id.min_tens_layout);
        splicer_param_layout = (RelativeLayout) v.findViewById(R.id.splicer_param_layout);
        btn_work_param = (Button) v.findViewById(R.id.btn_work_param);
        btn_cycle_param = (Button) v.findViewById(R.id.btn_cycle_param);
        btn_tension_param = (Button) v.findViewById(R.id.btn_tension_param);
        btn_anti_ptn_param = (Button) v.findViewById(R.id.btn_anti_ptn_param);
        btn_splicer_param = (Button) v.findViewById(R.id.btn_splicer_param);
        btn_yarn_suction_param = v.findViewById(R.id.btn_yarn_suction_param);
        btn_tens_sensor_has = (Button) v.findViewById(R.id.btn_tens_sensor_has);
        btn_tens_sensor_no = (Button) v.findViewById(R.id.btn_tens_sensor_no);
        btn_vss_has = (Button) v.findViewById(R.id.btn_vss_has);
        btn_vss_no = (Button) v.findViewById(R.id.btn_vss_no);
        btn_action_type_ok = v.findViewById(R.id.btn_action_type_ok);
        btn_action_type_special = v.findViewById(R.id.btn_action_type_special);

        OnClickListener scrollListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_work_param:
                        scrollView.smoothScrollTo(0, 0);
                        break;
                    case R.id.btn_cycle_param:
                        blow_time_edit.setFocusable(true);
                        scrollView.smoothScrollTo(0, (int) pageSeperator2);
                        break;
                    case R.id.btn_tension_param:
                        scrollView.smoothScrollTo(0, (int) pageSeperator3);
                        break;
                    case R.id.btn_anti_ptn_param:
                        scrollView.smoothScrollTo(0, (int) pageSeperator4);
                        break;
                    case R.id.btn_splicer_param:
                        scrollView.smoothScrollTo(0, (int) pageSeperator5);
                        break;
                    case R.id.btn_yarn_suction_param:
                        scrollView.smoothScrollTo(0, (int) pageSeperator6);
                        break;
                }
            }
        };
        btn_work_param.setOnClickListener(scrollListener);
        btn_cycle_param.setOnClickListener(scrollListener);
        btn_tension_param.setOnClickListener(scrollListener);
        btn_anti_ptn_param.setOnClickListener(scrollListener);
        btn_splicer_param.setOnClickListener(scrollListener);
        btn_yarn_suction_param.setOnClickListener(scrollListener);

        btn_tens_sensor_has.setOnClickListener(this);
        btn_tens_sensor_no.setOnClickListener(this);
        btn_vss_has.setOnClickListener(this);
        btn_vss_no.setOnClickListener(this);
        btn_action_type_ok.setOnClickListener(this);
        btn_action_type_special.setOnClickListener(this);
        btn_work_param.setActivated(true);
        group_value = (TextView) v.findViewById(R.id.group_value);
        group_value.setText("1");

        arm_lift_delay = initEdittextOnCreate(R.id.arm_lift_delay_value);
        full_package_brake_value = v.findViewById(R.id.full_package_brake_value);
        btn_full_package_brake = v.findViewById(R.id.btn_full_package_brake);
        btn_full_package_brake.setOnClickListener(this);

        wind_spd_edit = initEdittextOnCreate(R.id.wind_spd_value);
        start_acc_time_middle_edit = initEdittextOnCreate(R.id.start_acc_time_middle_value);
        start_acc_time_big_edit = initEdittextOnCreate(R.id.start_acc_time_big_value);
        start_acc_time_small_edit = initEdittextOnCreate(R.id.start_acc_time_small_value);

        s_drum_rev_spd_edit = initEdittextOnCreate(R.id.s_drum_rev_spd_value);
        l_drum_rev_spd_edit = initEdittextOnCreate(R.id.l_drum_rev_spd_value);
        splice_auto_start_edit = initEdittextOnCreate(R.id.splice_auto_start_value);
        cop_auto_start_edit = initEdittextOnCreate(R.id.cop_auto_start_value);

        cycle_num_edit = initEdittextOnCreate(R.id.cycle_num_value);
        pkg_num_edit = initEdittextOnCreate(R.id.pkg_num_value);
        change_num_edit = initEdittextOnCreate(R.id.change_num_value);

        anti_ptn_period_edit = initEdittextOnCreate(R.id.anti_ptn_period_value);
        acc_spd_edit = initEdittextOnCreate(R.id.acc_spd_value);
        acc_time_edit = initEdittextOnCreate(R.id.acc_time_per_value);

        btn_anti_ptn_mode = (ImageButton) v.findViewById(R.id.btn_anti_ptn_mode);
        btn_anti_ptn_mode.setOnClickListener(this);

        pkg_len_edit = initEdittextOnCreate(R.id.pkg_len_value);
        cop_len_edit = initEdittextOnCreate(R.id.cop_len_value);
        package_diameter_radio_edit = initEdittextOnCreate(R.id.package_diameter_radio_value);

        max_pkg_splice_edit = initEdittextOnCreate(R.id.max_pkg_splice_value);
        max_cop_cuts_edit = initEdittextOnCreate(R.id.max_cop_cuts_value);
        blow_time_edit = initEdittextOnCreate(R.id.blow_time_value);

        yarn_len_edit = initEdittextOnCreate(R.id.yarn_len_value);
        spd_reduce_edit = initEdittextOnCreate(R.id.spd_reduce_value);

        btn_anti_wrap_sense = (ImageButton) v.findViewById(R.id.btn_anti_wrap_sense);
        btn_anti_wrap_sense.setOnClickListener(this);

        min_tens_edit = initEdittextOnCreate(R.id.min_tens_value);
        // tens_sensor_edit = initEdittextOnCreate(R.id.tens_sensor_value);
        tension_edit = initEdittextOnCreate(R.id.tension_value_value);
        tens_increase_edit = initEdittextOnCreate(R.id.tens_increase_value);
        tens_low_lmt_edit = initEdittextOnCreate(R.id.tens_low_lmt_value);
        tens_up_lmt_edit = initEdittextOnCreate(R.id.tens_up_lmt_value);

        pkg_init_spd_edit = initEdittextOnCreate(R.id.pkg_init_spd_value);
        yarn_cnt_edit = initEdittextOnCreate(R.id.yarn_cnt_value);
        slow_brake_dec_time_edit = initEdittextOnCreate(R.id.slow_brake_dec_time_value);

        over_spd_inc_edit = initEdittextOnCreate(R.id.over_spd_inc_value);
        no_over_cycle_inc_edit = initEdittextOnCreate(R.id.no_over_cycle_inc_value);

        tens_pad_spd_eidt = initEdittextOnCreate(R.id.tens_pad_spd_value);
        tension_increase_time = initEdittextOnCreate(R.id.tension_increase_time_value);

        ends_num_edit = initEdittextOnCreate(R.id.ends_num_value);
        yarn_code_edit = initEdittextOnCreate(R.id.yarn_code_value);
        pkg_wei_edit = initEdittextOnCreate(R.id.pkg_wei_value);
        cop_wei_edit = initEdittextOnCreate(R.id.cop_wei_value);
        slip_edit = initEdittextOnCreate(R.id.slip_value);
        prod_correct_edit = initEdittextOnCreate(R.id.prod_correct_value);
        single_strength_edit = initEdittextOnCreate(R.id.single_strength_value);
        cop_num_edit = initEdittextOnCreate(R.id.cop_num_value);
        tens_yarn_num_edit = initEdittextOnCreate(R.id.tens_yarn_num_value);
        sample_yarn_num_edit = initEdittextOnCreate(R.id.sample_yarn_num_value);
        sample_up_lmt_edit = initEdittextOnCreate(R.id.sample_up_lmt_value);
        sample_low_lmt_edit = initEdittextOnCreate(R.id.sample_low_lmt_value);
        yarn_name_value = initEdittextOnCreate(R.id.yarn_name_value);

        anti_ptn_mode_value = (TextView) v.findViewById(R.id.anti_ptn_mode_value);
        anti_wrap_sense_value = (TextView) v.findViewById(R.id.anti_wrap_sense_value);

        yarn_cnt_unit = (TextView) v.findViewById(R.id.yarn_cnt_unit);
        pkg_len_unit = (TextView) v.findViewById(R.id.pkg_len_unit);
        cop_len_unit = (TextView) v.findViewById(R.id.cop_len_unit);
        pkg_wei_unit = (TextView) v.findViewById(R.id.pkg_wei_unit);
        cop_wei_unit = (TextView) v.findViewById(R.id.cop_wei_unit);
        wind_spd_unit = (TextView) v.findViewById(R.id.wind_spd_unit);
        s_drum_rev_spd_unit = (TextView) v.findViewById(R.id.s_drum_rev_spd_unit);
        l_drum_rev_spd_unit = (TextView) v.findViewById(R.id.l_drum_rev_spd_unit);
        pkg_init_spd_unit = (TextView) v.findViewById(R.id.pkg_init_spd_unit);
        splicer_length_value = (TextView) v.findViewById(R.id.splicer_length_value);

        OnClickListener lrListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_right_arrow: {
                        if ((grp_select > 0) && (grp_select < craftCount)) {
                            grp_select++;
                            updateGroupChange();
                        }
                        break;
                    }
                    case R.id.btn_left_arrow: {
                        if ((grp_select > 1) && (grp_select <= craftCount)) {
                            grp_select--;
                            updateGroupChange();
                        }
                        break;
                    }
                }
            }
        };

        btn_left_arrow = (ImageButton) v.findViewById(R.id.btn_left_arrow);
        btn_left_arrow.setOnClickListener(lrListener);
        btn_right_arrow = (ImageButton) v.findViewById(R.id.btn_right_arrow);
        btn_right_arrow.setOnClickListener(lrListener);

        splicer_time_t1_value = initEdittextOnCreate(R.id.splicer_time_t1_value);

        splicer_time_t2_value = initEdittextOnCreate(R.id.splicer_time_t2_value);

        btn_splicer_length = (ImageButton) v.findViewById(R.id.btn_splicer_length);
        btn_splicer_length.setOnClickListener(this);

        wind_spd_text = (TextView) v.findViewById(R.id.wind_spd_text);
        blow_time_text = (TextView) v.findViewById(R.id.blow_time_text);
        tens_sensor_text = (TextView) v.findViewById(R.id.tens_sensor_text);
        yarn_name_text = (TextView) v.findViewById(R.id.yarn_name_text);

        defect1 = v.findViewById(R.id.defect1);
        defect2 = v.findViewById(R.id.defect2);
        defect3 = v.findViewById(R.id.defect3);
        defect4 = v.findViewById(R.id.defect4);
        defect5 = v.findViewById(R.id.defect5);
        defect1.setOnClickListener(this);
        defect2.setOnClickListener(this);
        defect3.setOnClickListener(this);
        defect4.setOnClickListener(this);
        defect5.setOnClickListener(this);
        alarm1 = v.findViewById(R.id.alarm1);
        alarm2 = v.findViewById(R.id.alarm2);
        alarm3 = v.findViewById(R.id.alarm3);
        alarm4 = v.findViewById(R.id.alarm4);
        alarm5 = v.findViewById(R.id.alarm5);
        alarm1.setOnClickListener(this);
        alarm2.setOnClickListener(this);
        alarm3.setOnClickListener(this);
        alarm4.setOnClickListener(this);
        alarm5.setOnClickListener(this);
        suction_time_edit = initEdittextOnCreate(R.id.suction_time_value);

        repeat_time_edit = initEdittextOnCreate(R.id.repeat_time_value);

        suction_stroke_edit = initEdittextOnCreate(R.id.suction_stroke_value);

        tries_limit_edit = initEdittextOnCreate(R.id.tries_limit_value);

    }

    private void updateViews(SpindleParameter parameter) {

        if (btn_anti_ptn_param.isActivated()) {
            btn_anti_ptn_mode.setFocusableInTouchMode(true);
            btn_anti_ptn_mode.requestFocusFromTouch();
        } else if (btn_work_param.isActivated()) {
            wind_spd_text.setFocusableInTouchMode(true);
            wind_spd_text.requestFocusFromTouch();
        } else if (btn_cycle_param.isActivated()) {
            blow_time_text.setFocusableInTouchMode(true);
            blow_time_text.requestFocusFromTouch();
        } else if (btn_tension_param.isActivated()) {
            tens_sensor_text.setFocusableInTouchMode(true);
            tens_sensor_text.requestFocusFromTouch();
        } else if (btn_splicer_param.isActivated()) {
            yarn_name_text.setFocusableInTouchMode(true);
            yarn_name_text.requestFocusFromTouch();
        }

        if (sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0").equals("0")) {
            yarn_cnt_unit.setText(R.string.behind_ne);
        } else if (sp.getValue(MachineParameterSharedPreference.SYSTEM_COUNT_UNIT, "0").equals("1")) {
            yarn_cnt_unit.setText(R.string.behind_nm);
        } else {
            yarn_cnt_unit.setText(R.string.behind_tex);
        }

        if (sp.getValue(MachineParameterSharedPreference.SYSTEM_LENGTH_UNIT, "0").equals("0")) {
            pkg_len_unit.setText(R.string.ss_lengthUnit);
            cop_len_unit.setText(R.string.ss_lengthUnit);
            wind_spd_unit.setText(R.string.ss_speedUnit);
            s_drum_rev_spd_unit.setText(R.string.ss_speedUnit);
            l_drum_rev_spd_unit.setText(R.string.ss_speedUnit);
            pkg_init_spd_unit.setText(R.string.ss_speedUnit);
        } else {
            pkg_len_unit.setText(R.string.behind_y);
            cop_len_unit.setText(R.string.behind_y);
            wind_spd_unit.setText(R.string.behind_ymin);
            s_drum_rev_spd_unit.setText(R.string.behind_ymin);
            l_drum_rev_spd_unit.setText(R.string.behind_ymin);
            pkg_init_spd_unit.setText(R.string.behind_ymin);
        }

        if (sp.getValue(MachineParameterSharedPreference.SYSTEM_WEIGHT_UNIT, "0").equals("0")) {
            pkg_wei_unit.setText(R.string.ss_weightUnit);
            cop_wei_unit.setText(R.string.ss_weightUnit);
        } else {
            pkg_wei_unit.setText(R.string.ss_weightUnit_eng);
            cop_wei_unit.setText(R.string.ss_weightUnit_eng);
        }

        arm_lift_delay.setText(parameter.frame15DB);
        boolean fullPackageBrake = Utility.BitUtils.getBitValue(Integer.valueOf(parameter.frame15DC), 1);
        String[] fullPackageBrakeContent = getResources().getStringArray(R.array.full_package_brake_type);
        if (!fullPackageBrake) {
            full_package_brake_value.setText(fullPackageBrakeContent[0]);
        } else {
            full_package_brake_value.setText(fullPackageBrakeContent[1]);
        }

        start_acc_time_middle_edit.setText(parameter.frame20DC);
        start_acc_time_big_edit.setText(parameter.frame10DB);
        start_acc_time_small_edit.setText(parameter.frame10DC);

        tries_limit_edit.setText(parameter.frame16DCL);
        suction_time_edit.setText(parameter.frame16DCH);
        suction_stroke_edit.setText(parameter.frame16DDL);
        repeat_time_edit.setText(parameter.frame16DDH);

        splice_auto_start_edit.setText(parameter.frame21DDH);
        cop_auto_start_edit.setText(parameter.frame21DDL);

        cycle_num_edit.setText(parameter.frame22DB);
        pkg_num_edit.setText(parameter.frame22DC);
        change_num_edit.setText(parameter.frame22DD);

        anti_ptn_period_edit.setText(parameter.frame23DB);
        acc_spd_edit.setText(parameter.frame23DCH);
        acc_time_edit.setText(parameter.frame23DCL);

        String s_mode = parameter.frame23DD;
        int mode = Integer.parseInt(s_mode);
        String modeUI = "";
        if (mode == 0) {
            modeUI = getResources().getString(R.string.ordinary);
        } else {
            modeUI = getResources().getString(R.string.enhanced);
        }
        anti_ptn_mode_value.setText(modeUI);

        package_diameter_radio_edit.setText(parameter.frame24DD);

        max_pkg_splice_edit.setText(parameter.frame25DB);
        max_cop_cuts_edit.setText(parameter.frame25DC);
        blow_time_edit.setText(parameter.frame25DD);

        yarn_len_edit.setText(parameter.frame26DB);
        spd_reduce_edit.setText(parameter.frame26DC);

        String s_sense = parameter.frame26DD;
        int sense = Integer.parseInt(s_sense);
        String senseUI = "";
        switch (sense) {
            case 1:
                senseUI = "0";
                break;
            case 2:
                senseUI = "10";
                break;
            case 4:
                senseUI = "20";
                break;
            case 8:
                senseUI = "30";
                break;
            case 16:
                senseUI = "40";
                break;
            case 32:
                senseUI = "50";
                break;
            case 64:
                senseUI = "70";
                break;
            default:
                break;
        }
        anti_wrap_sense_value.setText(senseUI);

        min_tens_edit.setText(parameter.frame27DBH);
        tension_edit.setText(parameter.frame27DCH);
        tens_increase_edit.setText(parameter.frame27DCL);
        tens_low_lmt_edit.setText(parameter.frame27DDH);
        tens_up_lmt_edit.setText(parameter.frame27DDL);

        slow_brake_dec_time_edit.setText(parameter.frame28DD);

        over_spd_inc_edit.setText(parameter.frame2ADB);
        no_over_cycle_inc_edit.setText(parameter.frame2ADC);

        splicer_time_t1_value.setText(parameter.frame2CDB);
        splicer_length_value.setText(parameter.frame2CDD);
        splicer_time_t2_value.setText(parameter.frame2CDC);

        tens_pad_spd_eidt.setText(parameter.frame2EDB);
        tension_increase_time.setText(parameter.frame2EDC);

        ends_num_edit.setText(parameter.endsNum);
        yarn_code_edit.setText(parameter.yarnCode);
        slip_edit.setText(parameter.slipCoefficient);
        prod_correct_edit.setText(parameter.productionCorrectFactor);
        single_strength_edit.setText(parameter.singleStrength);
        cop_num_edit.setText(parameter.copSuckCount);
        tens_yarn_num_edit.setText(parameter.sensorYarnTouchTimes);
        sample_yarn_num_edit.setText(parameter.sampleCopCount);
        sample_up_lmt_edit.setText(parameter.sampleCopUplimit);
        sample_low_lmt_edit.setText(parameter.sampleCopLowlimit);
        yarn_name_value.setText(parameter.yarnName);

        float yarn_value = Float.parseFloat(parameter.frame28DC);
        yarn_cnt_edit.setText(String.format("%.1f", UnitUtils.getUICount(yarn_value)));

        pkg_len_edit.setText(UnitUtils.getUILength(parameter.frame24DB));
        cop_len_edit.setText(UnitUtils.getUILength(parameter.frame24DC));
        wind_spd_edit.setText(UnitUtils.getUILength(parameter.frame20DB));
        s_drum_rev_spd_edit.setText(UnitUtils.getUILength(parameter.frame21DB));
        l_drum_rev_spd_edit.setText(UnitUtils.getUILength(parameter.frame21DC));
        pkg_init_spd_edit.setText(UnitUtils.getUILength(parameter.frame28DB));

        String pkg_wei_value = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DB), parameter);
        String cop_wei_value = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DC), parameter);
        pkg_wei_edit.setText(pkg_wei_value);
        cop_wei_edit.setText(cop_wei_value);

        if (parameter.frame27DBL.equals("0")) {
            btn_tens_sensor_has.setSelected(false);
            btn_tens_sensor_no.setSelected(true);
            tens_up_lmt_layout.setVisibility(View.INVISIBLE);
            tens_low_lmt_layout.setVisibility(View.INVISIBLE);
            sample_yarn_num_layout.setVisibility(View.INVISIBLE);
            sample_up_lmt_layout.setVisibility(View.INVISIBLE);
            sample_low_lmt_layout.setVisibility(View.INVISIBLE);
        } else {
            btn_tens_sensor_has.setSelected(true);
            btn_tens_sensor_no.setSelected(false);
            tens_up_lmt_layout.setVisibility(View.VISIBLE);
            tens_low_lmt_layout.setVisibility(View.VISIBLE);
            sample_yarn_num_layout.setVisibility(View.VISIBLE);
            sample_up_lmt_layout.setVisibility(View.VISIBLE);
            sample_low_lmt_layout.setVisibility(View.VISIBLE);
        }

        boolean vss = Utility.BitUtils.getBitValue(Integer.valueOf(parameter.frame15DC), 2);
        if (!vss) {
            btn_vss_has.setSelected(false);
            btn_vss_no.setSelected(true);
            spd_reduce_layout.setVisibility(View.INVISIBLE);
            yarn_len_layout.setVisibility(View.INVISIBLE);
            min_tens_layout.setVisibility(View.INVISIBLE);
        } else {
            btn_vss_has.setSelected(true);
            btn_vss_no.setSelected(false);
            spd_reduce_layout.setVisibility(View.VISIBLE);
            yarn_len_layout.setVisibility(View.VISIBLE);
            min_tens_layout.setVisibility(View.VISIBLE);
        }

        int splicerType = (Integer.valueOf(parameter.frame15DC) & 8) >> 3;
        if (splicerType == 0) {
            splicer_param_layout.setVisibility(View.VISIBLE);
            btn_splicer_param.setVisibility(View.VISIBLE);
        } else {
            splicer_param_layout.setVisibility(View.INVISIBLE);
            btn_splicer_param.setVisibility(View.INVISIBLE);
        }

        int data16 = Integer.valueOf(parameter.frame16DB);

        if (Utility.BitUtils.getBitValue(data16, 0)) {
            btn_action_type_ok.setSelected(false);
            btn_action_type_special.setSelected(true);
            yarn_suction_detail_layout.setVisibility(View.VISIBLE);
        } else {
            btn_action_type_ok.setSelected(true);
            btn_action_type_special.setSelected(false);
            yarn_suction_detail_layout.setVisibility(View.INVISIBLE);
        }
        if (Utility.BitUtils.getBitValue(data16, 1)) {
            defect1.setChecked(true);
        } else {
            defect1.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 2)) {
            defect2.setChecked(true);
        } else {
            defect2.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 3)) {
            defect3.setChecked(true);
        } else {
            defect3.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 4)) {
            defect4.setChecked(true);
        } else {
            defect4.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 5)) {
            defect5.setChecked(true);
        } else {
            defect5.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 6)) {
            alarm1.setChecked(true);
        } else {
            alarm1.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 7)) {
            alarm2.setChecked(true);
        } else {
            alarm2.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 8)) {
            alarm3.setChecked(true);
        } else {
            alarm3.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 9)) {
            alarm4.setChecked(true);
        } else {
            alarm4.setChecked(false);
        }
        if (Utility.BitUtils.getBitValue(data16, 10)) {
            alarm5.setChecked(true);
        } else {
            alarm5.setChecked(false);
        }


    }

    private EditText initEdittextOnCreate(int id) {
        EditText editText = (EditText) v.findViewById(id);
        editText.setSelectAllOnFocus(true);
        editText.setOnFocusChangeListener(this);
        editText.setOnEditorActionListener(this);
        editText.setBackgroundDrawable(null);
        return editText;
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx,
                                int oldy) {
        if (y < pageSeperator2) {
            onSetButtonTrue(btn_work_param);
        } else if (y < pageSeperator3 && y >= pageSeperator2) {
            onSetButtonTrue(btn_cycle_param);
        } else if (y < pageSeperator4 && y >= pageSeperator3) {
            onSetButtonTrue(btn_tension_param);
        } else if (y < pageSeperator5 && y >= pageSeperator4) {
            onSetButtonTrue(btn_anti_ptn_param);
        } else if (y < pageSeperator6 && y >= pageSeperator5) {
            onSetButtonTrue(btn_splicer_param);
        } else if (y >= pageSeperator6) {
            onSetButtonTrue(btn_yarn_suction_param);
        }
    }

    private void onSetButtonTrue(Button button) {
        btn_work_param.setActivated(false);
        btn_cycle_param.setActivated(false);
        btn_tension_param.setActivated(false);
        btn_anti_ptn_param.setActivated(false);
        btn_splicer_param.setActivated(false);
        btn_yarn_suction_param.setActivated(false);
        button.setActivated(true);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
        } else {
            grp_select = 1;
            updateGroupChange();

        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        // 修改时，新建单锭参数
        long id = GroupMap.getInstance().getSpindleParameterIdFromGroup(grp_select);
        parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
        switch (viewId) {
            case R.id.btn_full_package_brake: {
                final String[] content = getResources().getStringArray(R.array.full_package_brake_type);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        full_package_brake_value.setText(content[position]);
                        int intValue = Integer.valueOf(parameter.frame15DC);
                        intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame15DC), 1, position);
                        parameter.frame15DC = String.valueOf(intValue);
                        List<byte[]> cmdList = new ArrayList<>();
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                        SmaroWindingMachineActivity.canHandStack
                                .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());

                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");

                break;
            }
            case R.id.btn_anti_ptn_mode: {
                final String[] content = getResources().getStringArray(R.array.anti_ptn_mode);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        anti_ptn_mode_value.setText(content[position]);

                        String strValue = "";
                        boolean isValid = false;
                        if (position == 0) {
                            strValue = "0";
                        } else {
                            strValue = "170";// 0xaa=170
                        }

                        String key = String.valueOf(0x23) + "-" + 3;
                        isValid = true;
                        if (isValid) {
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            parameter.frame23DD = strValue;
                            List<byte[]> cmdList = new ArrayList<>();
                            cmdList.add(CanCommand.getBus());
                            List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                            for (Integer groupNo : groupNos) {
                                cmdList.addAll(CanCommand.createGroupCmd23(groupNo, parameter));
                            }
                            cmdList.add(CanCommand.releaseBus());
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                            WaitProgressDialog.show(getActivity());
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");

                break;
            }

            case R.id.btn_anti_wrap_sense: {
                final String[] content = getResources().getStringArray(R.array.anti_wrap_sense);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        anti_wrap_sense_value.setText(content[position]);

                        boolean isValid = false;
                        int value = (1 << position);
                        String strValue = String.valueOf(value);

                        String key = String.valueOf(0x26) + "-" + 2;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                        if (isValid) {
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            parameter.frame26DD = strValue;
                            List<byte[]> cmdList = new ArrayList<>();
                            cmdList.add(CanCommand.getBus());
                            List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                            for (Integer groupNo : groupNos) {
                                cmdList.addAll(CanCommand.createGroupCmd26(groupNo, parameter));
                            }
                            cmdList.add(CanCommand.releaseBus());
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                            WaitProgressDialog.show(getActivity());
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_tens_sensor_has: {
                btn_tens_sensor_has.setSelected(true);
                btn_tens_sensor_no.setSelected(false);
                tens_up_lmt_layout.setVisibility(View.VISIBLE);
                tens_low_lmt_layout.setVisibility(View.VISIBLE);
                sample_yarn_num_layout.setVisibility(View.VISIBLE);
                sample_up_lmt_layout.setVisibility(View.VISIBLE);
                sample_low_lmt_layout.setVisibility(View.VISIBLE);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                parameter.frame27DBL = "85";
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.btn_tens_sensor_no: {
                btn_tens_sensor_has.setSelected(false);
                btn_tens_sensor_no.setSelected(true);
                tens_up_lmt_layout.setVisibility(View.INVISIBLE);
                tens_low_lmt_layout.setVisibility(View.INVISIBLE);
                sample_yarn_num_layout.setVisibility(View.INVISIBLE);
                sample_up_lmt_layout.setVisibility(View.INVISIBLE);
                sample_low_lmt_layout.setVisibility(View.INVISIBLE);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                parameter.frame27DBL = "0";
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
                break;
            }
            case R.id.btn_vss_has: {
                btn_vss_has.setSelected(true);
                btn_vss_no.setSelected(false);
                spd_reduce_layout.setVisibility(View.VISIBLE);
                yarn_len_layout.setVisibility(View.VISIBLE);
                min_tens_layout.setVisibility(View.VISIBLE);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                String para0x15DC = parameter.frame15DC;
                int vss = Utility.BitUtils.setBitValue(Integer.valueOf(para0x15DC), 2, 1);
                parameter.frame15DC = String.valueOf(vss);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
            }
            break;
            case R.id.btn_vss_no: {
                btn_vss_has.setSelected(false);
                btn_vss_no.setSelected(true);
                spd_reduce_layout.setVisibility(View.INVISIBLE);
                yarn_len_layout.setVisibility(View.INVISIBLE);
                min_tens_layout.setVisibility(View.INVISIBLE);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                String para0x15DC = parameter.frame15DC;
                int vss = Utility.BitUtils.setBitValue(Integer.valueOf(para0x15DC), 2, 0);
                parameter.frame15DC = String.valueOf(vss);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());
            }
            break;
            case R.id.btn_splicer_length: {
                final String[] content = getResources().getStringArray(R.array.splicer_time_t1);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        splicer_length_value.setText(content[position]);

                        String strValue = "";
                        boolean isValid = false;
                        strValue = String.valueOf(position + 1);
                        String key = String.valueOf(0x2c) + "-" + 2;
                        isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                        if (isValid) {
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            SmaroWindingMachineActivity.canHandStack
                                    .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                            parameter.frame2CDD = strValue;
                            List<byte[]> cmdList = new ArrayList<>();
                            cmdList.add(CanCommand.getBus());
                            List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                            for (Integer groupNo : groupNos) {
                                cmdList.addAll(CanCommand.createGroupCmd2C(groupNo, parameter));
                            }
                            cmdList.add(CanCommand.releaseBus());
                            SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                            WaitProgressDialog.show(getActivity());
                        }
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
                break;
            }
            case R.id.btn_action_type_ok: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 0, 0);
                parameter.frame16DB = String.valueOf(intValue);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                cmdList.addAll(CanCommand.createGroupCmd16(grp_select, parameter));
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                btn_action_type_ok.setSelected(true);
                btn_action_type_special.setSelected(false);
                yarn_suction_detail_layout.setVisibility(View.INVISIBLE);
                WaitProgressDialog.show(getActivity());
            }
            break;
            case R.id.btn_action_type_special: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 0, 1);
                parameter.frame16DB = String.valueOf(intValue);
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                btn_action_type_ok.setSelected(false);
                btn_action_type_special.setSelected(true);
                yarn_suction_detail_layout.setVisibility(View.VISIBLE);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.defect1: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 1)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 1, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 1, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.defect2: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 2)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 2, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 2, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.defect3: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 3)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 3, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 3, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.defect4: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 4)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 4, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 4, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.defect5: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 5)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 5, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 5, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.alarm1: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 6)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 6, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 6, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.alarm2: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 7)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 7, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 7, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.alarm3: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 8)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 8, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 8, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.alarm4: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 9)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 9, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 9, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            case R.id.alarm5: {
                WaitProgressDialog.show(getActivity());
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteFailedListener(ParameterSettingSpindleParameterFragment_craft.this);
                SmaroWindingMachineActivity.canHandStack
                        .registerWriteSucceedListener(ParameterSettingSpindleParameterFragment_craft.this);
                int intValue = Integer.valueOf(parameter.frame16DB);
                if (Utility.BitUtils.getBitValue(intValue, 10)) {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 10, 0);
                    parameter.frame16DB = String.valueOf(intValue);
                } else {
                    intValue = Utility.BitUtils.setBitValue(Integer.valueOf(parameter.frame16DB), 10, 1);
                    parameter.frame16DB = String.valueOf(intValue);
                }
                List<byte[]> cmdList = new ArrayList<>();
                cmdList.add(CanCommand.getBus());
                List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                for (Integer groupNo : groupNos) {
                    cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                }
                cmdList.add(CanCommand.releaseBus());
                SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                WaitProgressDialog.show(getActivity());

            }
            break;
            default:
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            SmaroWindingMachineActivity.canHandStack.registerWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.registerWriteSucceedListener(this);
            String strValue = v.getText().toString();
            if (strValue.equals("")) {
                return false;
            }
            // 修改时，新建单锭参数
            long id = GroupMap.getInstance().getSpindleParameterIdFromGroup(grp_select);
            parameter = ObjectBox.get().boxFor(SpindleParameter.class).get(id);
            boolean isValid = false;
            String key = "";
            switch (v.getId()) {
                case R.id.arm_lift_delay_value:
                    key = String.valueOf(0x15) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame15DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd15(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.wind_spd_value:
                    float wind_spd = Float.parseFloat(strValue);
                    float temp_wind_spd = UnitUtils.getSendLength(wind_spd);
                    int wind_spd_metre = Math.round(temp_wind_spd);
                    String strValueSpd = String.valueOf(wind_spd_metre);

                    key = String.valueOf(0x20) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueSpd);
                    if (isValid) {
                        parameter.frame20DB = strValueSpd;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd20(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.start_acc_time_middle_value:
                    key = String.valueOf(0x20) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame20DC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd20(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.start_acc_time_big_value:
                    key = String.valueOf(0x10) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame10DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd10(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.start_acc_time_small_value:
                    key = String.valueOf(0x10) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame10DC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd10(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.s_drum_rev_spd_value:
                    float s_drum_rev_spd = Float.parseFloat(strValue);
                    float temp_s_drum_rev_spd = UnitUtils.getSendLength(s_drum_rev_spd);
                    int s_drum_rev_spd_metre = Math.round(temp_s_drum_rev_spd);
                    String strValueSSpd = String.valueOf(s_drum_rev_spd_metre);

                    key = String.valueOf(0x21) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueSSpd);
                    if (isValid) {
                        parameter.frame21DB = strValueSSpd;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd21(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.l_drum_rev_spd_value:
                    float l_drum_rev_spd = Float.parseFloat(strValue);
                    float temp_l_drum_rev_spd = UnitUtils.getSendLength(l_drum_rev_spd);
                    int l_drum_rev_spd_metre = Math.round(temp_l_drum_rev_spd);
                    String strValueLSpd = String.valueOf(l_drum_rev_spd_metre);

                    key = String.valueOf(0x21) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueLSpd);
                    if (isValid) {
                        parameter.frame21DC = strValueLSpd;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd21(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.splice_auto_start_value:
                    key = String.valueOf(0x21) + "-" + 3;// key互调决定高低位互调 DC.H
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame21DDH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd21(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.cop_auto_start_value:
                    key = String.valueOf(0x21) + "-" + 2;// key互调决定高低位互调 DC.L
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame21DDL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd21(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.cycle_num_value:
                    key = String.valueOf(0x22) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame22DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd22(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.pkg_num_value:
                    key = String.valueOf(0x22) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame22DC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd22(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.change_num_value:
                    key = String.valueOf(0x22) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame22DD = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd22(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.anti_ptn_period_value:
                    key = String.valueOf(0x23) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame23DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd23(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.acc_spd_value:
                    key = String.valueOf(0x23) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame23DCH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd23(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.acc_time_per_value:
                    key = String.valueOf(0x23) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame23DCL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd23(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.pkg_len_value: {
                    float pkg_len = Float.parseFloat(strValue);
                    float temp_metre = UnitUtils.getSendLength(pkg_len);
                    int pkg_len_metre = Math.round(temp_metre);

                    String strValueLen = String.valueOf(pkg_len_metre);
                    key = String.valueOf(0x24) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueLen);

                    if (isValid) {
                        parameter.frame24DB = strValueLen;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;
                case R.id.cop_len_value: {
                    float cop_len = Float.parseFloat(strValue);
                    float temp_metre = UnitUtils.getSendLength(cop_len);
                    int cop_len_metre = Math.round(temp_metre);// 不必转换成脉冲值

                    String strValueLen = String.valueOf(cop_len_metre);
                    key = String.valueOf(0x24) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueLen);

                    if (isValid) {
                        parameter.frame24DC = strValueLen;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;
                case R.id.package_diameter_radio_value: {
                    key = String.valueOf(0x24) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);

                    if (isValid) {
                        parameter.frame24DD = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                }
                break;

                case R.id.max_pkg_splice_value:
                    key = String.valueOf(0x25) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame25DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd25(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.max_cop_cuts_value:
                    key = String.valueOf(0x25) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame25DC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd25(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.blow_time_value:
                    key = String.valueOf(0x25) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame25DD = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd25(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.yarn_len_value:
                    key = String.valueOf(0x26) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame26DB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd26(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.spd_reduce_value:
                    key = String.valueOf(0x26) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame26DC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd26(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.min_tens_value:
                    key = String.valueOf(0x27) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame27DBH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tension_value_value:
                    key = String.valueOf(0x27) + "-" + 3;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame27DCH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tens_increase_value:
                    key = String.valueOf(0x27) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame27DCL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tens_low_lmt_value:
                    key = String.valueOf(0x27) + "-" + 5;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame27DDH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tens_up_lmt_value:
                    key = String.valueOf(0x27) + "-" + 4;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame27DDL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd27(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.pkg_init_spd_value:
                    float pkg_init_spd = Float.parseFloat(strValue);
                    float temp_pkg_init_spd = UnitUtils.getSendLength(pkg_init_spd);
                    int pkg_init_spd_metre = Math.round(temp_pkg_init_spd);
                    String strValueISpd = String.valueOf(pkg_init_spd_metre);

                    key = String.valueOf(0x28) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValueISpd);
                    if (isValid) {
                        parameter.frame28DB = strValueISpd;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd28(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.yarn_cnt_value:
                    key = String.valueOf(0x28) + "-" + 1;
                    float yarn_cnt = Float.parseFloat(strValue);
                    float temp = UnitUtils.getSendCount(yarn_cnt);
                    String count = Report.round(String.valueOf(temp), 1);
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, String.valueOf(temp));

                    if (isValid) {
                        parameter.frame28DC = count;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd28(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.slow_brake_dec_time_value:
                    key = String.valueOf(0x28) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame28DD = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd28(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.over_spd_inc_value:
                    key = String.valueOf(0x2a) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2ADB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd2A(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.no_over_cycle_inc_value:
                    key = String.valueOf(0x2a) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2ADC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd2A(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;

                case R.id.tens_pad_spd_value:
                    key = String.valueOf(0x2e) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2EDB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd2E(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tension_increase_time_value:
                    key = String.valueOf(0x2e) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2EDC = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);

//                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
//                        SmaroWindingMachineActivity.canHandStack
//                                .putPriorityCmd(sp.createCanCmd(grp_select, 0x2e, 0x02, strValue));
//                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
//                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.ends_num_value:
                    key = MachineParameterSharedPreference.SPINDLE_ENDS_NUM;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.endsNum = strValue;
                        String pkg = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DB), parameter);
                        pkg_wei_edit.setText(pkg);
                        String cop = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DC), parameter);
                        cop_wei_edit.setText(cop);
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.yarn_code_value:
                    key = MachineParameterSharedPreference.SPINDLE_YARN_CODE;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.yarnCode = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.slip_value:
                    key = MachineParameterSharedPreference.SPINDLE_SLIP;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.slipCoefficient = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);

                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.prod_correct_value:
                    key = MachineParameterSharedPreference.SPINDLE_PCF;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.productionCorrectFactor = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                        String pkg = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DB), parameter);
                        pkg_wei_edit.setText(pkg);
                        String cop = UnitUtils.getUIWeightFromLength(Integer.parseInt(parameter.frame24DC), parameter);
                        cop_wei_edit.setText(cop);
                    }
                    break;
                case R.id.single_strength_value:
                    key = MachineParameterSharedPreference.SPINDLE_SINGLE_STRENGTH;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.singleStrength = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.pkg_wei_value: {
                    int temp_g = (int) (Float.parseFloat(strValue) * 1000);
                    int pkg_gram = (int) (UnitUtils.getUIWeightFromWeight(temp_g) * 1000);
                    key = MachineParameterSharedPreference.SPINDLE_PKG_WEI;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, String.valueOf(temp_g));
                    int len = UnitUtils.getSendLengthFromWeight(pkg_gram, parameter);
                    if (isValid) {
                        parameter.frame24DB = String.valueOf(len);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                }
                case R.id.cop_wei_value: {
                    int temp_g = (int) (Float.parseFloat(strValue) * 1000);
                    int cop_gram = (int) (UnitUtils.getUIWeightFromWeight(temp_g) * 1000);
                    key = MachineParameterSharedPreference.SPINDLE_COP_WEI;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, String.valueOf(temp_g));
                    int cop_len = UnitUtils.getSendLengthFromWeight(cop_gram, parameter);
                    if (isValid) {
                        parameter.frame24DC = String.valueOf(cop_len);
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd24(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                }
                case R.id.tens_yarn_num_value:
                    key = MachineParameterSharedPreference.SPINDLE_TENS_YARN_NUM;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.sensorYarnTouchTimes = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.cop_num_value:
                    key = MachineParameterSharedPreference.SPINDLE_COP_NUM;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.copSuckCount = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.sample_yarn_num_value:
                    key = MachineParameterSharedPreference.SPINDLE_SAMPLE_YARN_NUM;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.sampleCopCount = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.sample_up_lmt_value:
                    key = MachineParameterSharedPreference.SPINDLE_SAMPLE_UP_LMT;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.sampleCopUplimit = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.sample_low_lmt_value:
                    key = MachineParameterSharedPreference.SPINDLE_SAMPLE_LOW_LMT;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.sampleCopLowlimit = strValue;
                        ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    }
                    break;
                case R.id.yarn_name_value:
                    key = MachineParameterSharedPreference.YARN_NAME;
                    isValid = true;
                    parameter.yarnName = strValue;
                    ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
                    break;
                case R.id.splicer_time_t1_value:
                    key = String.valueOf(0x2c) + "-" + 0;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2CDB = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd2C(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.splicer_time_t2_value:
                    key = String.valueOf(0x2c) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame2CDC = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd2C(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.tries_limit_value:
                    key = String.valueOf(0x16) + "-" + 1;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame16DCL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.suction_time_value:
                    key = String.valueOf(0x16) + "-" + 2;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame16DCH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.suction_stroke_value:
                    key = String.valueOf(0x16) + "-" + 3;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame16DDL = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;
                case R.id.repeat_time_value:
                    key = String.valueOf(0x16) + "-" + 4;
                    isValid = SmaroWindingMachineDataAccessRules.getInstance().isBetween(key, strValue);
                    if (isValid) {
                        parameter.frame16DDH = strValue;
                        List<byte[]> cmdList = new ArrayList<>();
                        cmdList.add(CanCommand.getBus());
                        List<Integer> groupNos = GroupMap.getInstance().getGroupNosFromID(parameter.id);
                        for (Integer groupNo : groupNos) {
                            cmdList.addAll(CanCommand.createGroupCmd16(groupNo, parameter));
                        }
                        cmdList.add(CanCommand.releaseBus());
                        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(cmdList);
                        WaitProgressDialog.show(getActivity());
                    }
                    break;


                default:
                    break;
            }
            if (!isValid) {
                SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
                SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
                String max = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMax();
                String min = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getMin();
                float max1, min1;
                int max2, min2;
                String warning = getResources().getString(R.string.range_warning);
                if (key.equals(String.valueOf(0x28) + "-" + 1)) {
                    max1 = Math.round(UnitUtils.getUICount(Float.parseFloat(max)) * 10) / 10f;
                    min1 = Math.round(UnitUtils.getUICount(Float.parseFloat(min)) * 10) / 10f;
                    warning = String.format(warning, String.valueOf(min1), String.valueOf(max1));
                } else if ((key.equals(String.valueOf(0x24) + "-" + 0))
                        || (key.equals(String.valueOf(0x24) + "-" + 1))) {
                    max2 = Integer.valueOf(UnitUtils.getUILength(max));
                    min2 = Integer.valueOf(UnitUtils.getUILength(min));
                    warning = String.format(warning, min2, max2);
                } else if ((key.equals(MachineParameterSharedPreference.SPINDLE_PKG_WEI))
                        || (key.equals(MachineParameterSharedPreference.SPINDLE_COP_WEI))) {
                    max1 = UnitUtils.getUIWeightFromWeight(Float.parseFloat(max));
                    min1 = UnitUtils.getUIWeightFromWeight(Float.parseFloat(min));
                    warning = String.format(warning, min1, max1);
                } else {
                    warning = String.format(warning, min, max);
                }
                WarningDialog.show(getActivity(), warning);
                updateGroupChange();
            }
        }
        return false;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        int id = v.getId();
        if (!hasFocus) {
            updateGroupChange();
        }
    }

    @Override
    public void writeFailed(ProtocolStack ps, byte[] request) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateGroupChange();
            }
        });
    }

    @Override
    public void writeSucceed(ProtocolStack ps, byte[] request) {
        short index = (short) (request[2] & 0xff);
        if (index == 0x24) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                byte[] data = {request[7], request[6]};
                ModbusCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand((byte) 1, 107, data));
            }
        } else if (index == 0x10 || index == 0x16 || index == 0x20 || index == 0x21 || index == 0x22
                || index == 0x23 || index == 0x25 || index == 0x26
                || index == 0x27 || index == 0x2a || index == 0x2c || index == 0x2e) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                if (index == 0x20) {
                    byte[] data = {request[5], request[4]};
                    ModbusCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand((byte) 1, 105, data));
                }
            }

        } else if (index == 0x28) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
            if (sp.getValue(MachineParameterSharedPreference.PLC_TYPE, "0").equals("0")) {
                byte[] data = {request[7], request[6]};
                ModbusCommandManager.getInstance().putPriorityCmd(CommandGenerator.createWriteCommand((byte) 1, 106, data));
            }
        } else if (index == 0x15) {
            SmaroWindingMachineActivity.canHandStack.unregisterWriteFailedListener(this);
            SmaroWindingMachineActivity.canHandStack.unregisterWriteSucceedListener(this);
            ObjectBox.get().boxFor(SpindleParameter.class).put(parameter);
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateGroupChange();
            }
        });
    }


    private void updateGroupChange() {
        // 拼接批次列表，优先按照分组顺序显示，其他批次依次显示
        Box<SpindleParameter> box = ObjectBox.get().boxFor(SpindleParameter.class);
        List<Group> groupList = GroupMap.getInstance().getGroupList();
        final List<SpindleParameter> sortedParameters = new ArrayList<>();
        for (Group group : groupList) {
            sortedParameters.add(box.get(group.getId()));
        }
        List<SpindleParameter> leftParameters = box.query().filter(new QueryFilter<SpindleParameter>() {
            @Override
            public boolean keep(SpindleParameter entity) {
                for (SpindleParameter parameter : sortedParameters) {
                    if (entity.craftName.equals(parameter.craftName)) {
                        return false;
                    }
                }
                return true;
            }
        }).build().find();
        sortedParameters.addAll(leftParameters);

        craftCount = sortedParameters.size();

        SpindleParameter parameter = sortedParameters.get(grp_select - 1);
        String groupCraft;
        if (grp_select <= groupList.size()) {
            groupCraft = getString(R.string.group) + ":" + grp_select + "\n"
                    + getString(R.string.lot) + ":" + parameter.craftName;
            setViewAndChildrenEnabled(scrollView, true);

        } else {
            // 未应用的批次在单锭设置页面不能修改
            groupCraft = getString(R.string.lot) + ":" + parameter.craftName;
            setViewAndChildrenEnabled(scrollView, false);
        }
        group_value.setText(groupCraft);
        updateViews(parameter);
    }

    private void setViewAndChildrenEnabled(View view, boolean enabled) {
        view.setEnabled(enabled);
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                setViewAndChildrenEnabled(child, enabled);
            }
        }
    }

}